﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using FundSms.Client.IDAL;
using FundSms.Client.Model;
using FundSms.DBUtility;

namespace FundSms.Client.SqlDAL
{
    public class SmsMO : ISmsMO
    {
        private const string SQL_INSERT_MO = "INSERT INTO TSMS_MO(MOBILE,SERVERCODE,CONTENT,GETTIME) VALUES (@MOBILE,@SERVERCODE,@CONTENT,@GETTIME)";
        private const string SQL_INSERT_MO_BATCH = "INSERT INTO TSMS_MO(MOBILE,SERVERCODE,CONTENT,GETTIME) VALUES ('{0}','{1}','{2}','{3}')";

        private const string PARM_MOBILE = "@MOBILE";
        private const string PARM_SERVERCODE = "@SERVERCODE";
        private const string PARM_CONTENT = "@CONTENT";
        private const string PARM_GETTIME = "@GETTIME";

        #region ISmsMO 成员

        public void Insert(TSMSMOInfo moinfo)
        {
            SqlParameter[] logparms = GetMOParameters();

            logparms[0].Value = moinfo.Mobile;
            logparms[1].Value = moinfo.ServiceCode;
            logparms[2].Value = moinfo.Content;
            logparms[3].Value = moinfo.GetTime;

            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringClientSms, CommandType.Text, SQL_INSERT_MO, logparms);
        }

        public void InsertBatch(string sql)
        {
            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringClientSms, CommandType.Text, sql, null);
        }

        public void InsertList(List<TSMSMOInfo> li)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("BEGIN");

            object[] obj = new object[11];

            int i = 1;

            foreach (TSMSMOInfo moinfo in li)
            {
                obj[0] = moinfo.Mobile;
                obj[1] = moinfo.ServiceCode;
                obj[2] = moinfo.Content;
                obj[3] = moinfo.GetTime;

                sb.Append(string.Format(SQL_INSERT_MO_BATCH, obj));
                sb.Append(";");

                i++;
            }

            sb.Append("END");

            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringClientSms, CommandType.Text, sb.ToString(), null);
        }

        /// <summary>
        /// 获得缓存的参数
        /// </summary>
        /// <returns></returns>
        private static SqlParameter[] GetMOParameters()
        {
            SqlParameter[] parms = SqlHelper.GetCachedParameters(SQL_INSERT_MO);

            if (parms == null)
            {
                parms = new SqlParameter[] {												 
					new SqlParameter(PARM_MOBILE, SqlDbType.VarChar, 1300),
					new SqlParameter(PARM_SERVERCODE, SqlDbType.VarChar, 140),
					new SqlParameter(PARM_CONTENT, SqlDbType.VarChar,2),
					new SqlParameter(PARM_GETTIME, SqlDbType.VarChar, 20)
					};

                SqlHelper.CacheParameters(SQL_INSERT_MO, parms);
            }

            return parms;
        }

        #endregion
    }
}
